package com.pxst.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxst.exception.BusinessException;
import com.pxst.resp.ApiResp;
import com.pxst.sys.entity.SysCurrencyConfigEntity;
import com.pxst.sys.mapper.SysCurrencyConfigMapper;
import com.pxst.sys.req.*;
import com.pxst.sys.service.SysCurrencyConfigService;
import com.pxst.utils.BuildQueryUtils;
import com.pxst.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


import static com.pxst.enums.RedisKeyEnums.CURRENCY_KEY;


@Service
@Transactional
public class SysCurrencyConfigServiceImpl extends ServiceImpl<SysCurrencyConfigMapper, SysCurrencyConfigEntity> implements SysCurrencyConfigService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysCurrencyConfigMapper currencyConfigMapper;

    @Override
    public ApiResp<String> currencyCreate(CurrencyCreateRsp currencyCreateRsp) {

        QueryWrapper<SysCurrencyConfigEntity> build = BuildQueryUtils.build(SysCurrencyConfigEntity.class, currencyCreateRsp);
        SysCurrencyConfigEntity one = getOne(build);
        if (!ObjectUtils.isEmpty(one)){
            throw new BusinessException("币种名已存在");
        }

        SysCurrencyConfigEntity sysCurrencyConfig = new SysCurrencyConfigEntity();

        BeanUtils.copyProperties(currencyCreateRsp,sysCurrencyConfig);
        save(sysCurrencyConfig);
        System.out.println(sysCurrencyConfig);

        String key = CURRENCY_KEY.getKey();
        redisUtils.pushList(key,sysCurrencyConfig.getCurrencyAbb());

        return ApiResp.sucess(sysCurrencyConfig);
    }

    @Override
    public ApiResp<String> currencyUpdate(CurrencyUpdateReq sysCurrencyConfigReq) {

        SysCurrencyConfigEntity currencyById = getById(sysCurrencyConfigReq.getId());

//        redisUtils.removeList(CURRENCY_KEY.getKey(),currencyById);
        CurrencyUpdateReq sysCurrencyConfig = new CurrencyUpdateReq();
        BeanUtils.copyProperties(currencyById,sysCurrencyConfig);
        System.out.println(sysCurrencyConfig);

        if (ObjectUtils.isEmpty(currencyById)) {
            throw new BusinessException(String.format("币种%s不存在", currencyById.getId()));
        }
        SysCurrencyConfigEntity one = getOne(new QueryWrapper<SysCurrencyConfigEntity>().lambda().eq(SysCurrencyConfigEntity::getCurrencyAbb, sysCurrencyConfigReq.getCurrencyAbb())
                .ne(SysCurrencyConfigEntity::getId, sysCurrencyConfigReq.getId()));
        if (!ObjectUtils.isEmpty(one)) {
            throw new BusinessException(String.format("币种缩写%s已存在", sysCurrencyConfigReq.getCurrencyAbb()));
        }

        BeanUtils.copyProperties(sysCurrencyConfigReq, currencyById);
        updateById(currencyById);

        redisUtils.removeList(CURRENCY_KEY.getKey(),sysCurrencyConfig);
        redisUtils.pushList(CURRENCY_KEY.getKey(),currencyById);

        return ApiResp.sucess();
    }
    @Override
    public ApiResp<String> currencyDelete(CurrencyDeleteRsp currencyReq) {
        SysCurrencyConfigEntity byId = getById(currencyReq.getId());
        redisUtils.removeList(CURRENCY_KEY.getKey(),byId);
//        SysCurrencyConfigEntity sysCurrencyConfig = new SysCurrencyConfigEntity();
//        BeanUtils.copyProperties(byId,sysCurrencyConfig);

        if (ObjectUtils.isEmpty(byId)){
            throw new BusinessException(String.format("币种%s不存在", byId.getId()));
        }
        removeById(currencyReq.getId());

        //删除redis


        return ApiResp.sucess();
    }

//    @Override
//    public ApiResp<String> currencySelect(CurrencySelectReq currencyPageReq) {
//        String currencyAbb = currencyPageReq.getCurrencyAbb();
//        System.out.println(currencyAbb);
//        String key = CURRENCY_KEY.getKey() + currencyPageReq.getCurrencyAbb();
//     //   CurrencyRedisRsp currencyRedisRsp = (CurrencyRedisRsp)redisUtils.get(key);
//        System.out.println(key);
//        if (ObjectUtils.isEmpty(key)){
//            throw new BusinessException(String.format("币种%s不存在", key));
//        }
//
//        return ApiResp.sucess(redisUtils.get(key));
//    }

    @Override
    public List<String> currencyList() {
        String key = CURRENCY_KEY.getKey();
        List<Object> allList = redisUtils.getAllList(key);
        if (ObjectUtils.isEmpty(allList) || allList.size() == 0){
            // 获取数据库的全部币种数据
            List<SysCurrencyConfigEntity> list = list();
            // 获取币种缩写信息并存入redis
            list.forEach(item -> {
                redisUtils.pushList(CURRENCY_KEY.getKey(), item.getCurrencyAbb());
            });
            return list.stream().map(SysCurrencyConfigEntity::getCurrencyAbb).collect(Collectors.toList());
        }
        return allList.stream().map(item -> (String)item).collect(Collectors.toList());
    }
}
